/**
 * 用户自定义需要显示的分类
 */

const $vm = getApp()
const util = $vm.utils

Page({
    data:{
        categorys:[],
        Mycatelist:{},//栏目
        userInfo:{}
    },
    onReady(){
        wx.setNavigationBarTitle({title:'设置关注分类'})
        // 本地数据
        this._getCategorys()
    },

    deleteCategory(event) {
        var that = this
        wx.getUserInfo({
            success: function (res) {
                var userInfo = res.userInfo;
                //this._setCategorySelected(event, false)
                // 其中头条为必选项
                var key = event.target.dataset.key
                if (parseInt(key, 10) === 0) {
                    wx.showToast({ title: '体育不可删除', duration: 2000 })
                    return false
                }
                wx.request({
                    url: 'http://localhost:8080/CategoryApi/delMycateAPI',
                    data: { 
                        cateId: event.currentTarget.dataset.id,
                         userInfo: userInfo
                         },
                    method: 'GET',
                    success: function (res) {
                        that.setData({
                            Mycatelist: res.data //我的栏目
                        })
                    }
                });
            }
        })

    },

    addCategory(event) {
        var that = this
        wx.getUserInfo({
            success: function (res) {
                var userInfo = res.userInfo;
                var chids = event.target.dataset.id
                wx.request({
                    url: 'http://localhost:8080/CategoryApi/addMycateAPI',
                    data: {
                        cateId: event.currentTarget.dataset.id,
                        userInfo: userInfo
                    },
                    method: 'GET',
                    success: function (res) {
                        that.setData({
                            Mycatelist: res.data, //我的栏目
                        })
                    }
                });
            }
        })

    },
    selectAll(){
        wx.setStorageSync('USER_COLLECT',[])
        $vm.globalData.categoryChanged = true
        this._getCategorys()
    },
    returnIndex(){
        wx.navigateBack()
    },
    _getCategorys(){
        util.getCategorys().then(res => this.setData({
            categorys:res
        }))
    },
    _setCategorySelected(event,isSelect){
        var key =  event.target.dataset.key
        var idx = `categorys[${key}].selected`

        // 其中头条为必选项
        if(parseInt(key,10) === 0){
            wx.showToast({title:'体育不可删除',duration:2000})
            return false
        }

        this.setData({ [idx] : isSelect })
        // 保存我的喜欢
        var likedArr = []
        this.data.categorys.forEach(function(category){
            if(category.selected){
                likedArr.push({
                    id:category.lanmu_id,
                    sort:0
                })
            }
        })
        $vm.globalData.categoryChanged = true
        wx.setStorageSync('USER_COLLECT',likedArr)
    },
    onLoad: function () {
    //栏目文章
        var that = this
        wx.getUserInfo({
            success: function (res) {
                var userInfo = res.userInfo;
                wx.request({
                    url: 'http://localhost:8080/CategoryApi/MycateAPI',
                    data: {
                        id: 1,
                        userInfo: userInfo
                    },
                    method: 'GET',
                    success: function (res) {
                        that.setData({
                           Mycatelist: res.data, //栏目
                        })
                    }
                });
            }
        })
  }
})